import nci60.data.protein.GeneName

//def data = [:]
//GeneName.executeQuery('select g.id, g.name, g.protein.id from GeneName g').each{line->
//    def id = line[0]
//    def name = line[1]
//    def prot = line[2]
//    
//    if(!data.containsKey(prot)){data.put(prot, [])}
//    data[prot].add([name:name, id:id])
//}

//def genesToUpdate = []
//data.each{prot, gene->
//    genesToUpdate.add(gene.sort{it.name.size()}[0].id)
//}
//
////genesToUpdate.each{println it}
//
//GeneName.executeUpdate('update GeneName set isMainName=true where id in :ids', [ids:genesToUpdate])

def data = [:]
GeneName.executeQuery('select g, g.name, g.protein.id from GeneName g').each{line->
    def gene = line[0]
    def name = line[1]
    def prot = line[2]
    
    if(!data.containsKey(prot)){data.put(prot, [])}
    data[prot].add([name:name, id:gene])
}

data.each{prot, gene->
    def g = gene.sort{it.name.size()}[0].gene
    g.isMainName = true
    if (!g.save(flush:true)) {
        g.errors.each {println it}
    }
    println ''+g.id+'\t'+g.name
}